package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.BusinessException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.util.JwtUtil;
import com.atguigu.lease.common.util.UUIDUtils;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import jakarta.annotation.Resource;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author liangtianzhe
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SystemUserMapper systemUserMapper;

    @Override
    public CaptchaVo getCaptcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
        String code = specCaptcha.text().toLowerCase();
        String image = specCaptcha.toBase64();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUIDUtils.generateUUID();
        stringRedisTemplate.opsForValue().set(key,code,60, TimeUnit.SECONDS);
        return CaptchaVo.builder().key(key).image(image).build();
    }

    @Override
    public String login(LoginVo loginVo) {
        String captchaCode = loginVo.getCaptchaCode();
        if(StringUtils.isBlank(captchaCode)){
            throw new BusinessException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        if(!stringRedisTemplate.hasKey(loginVo.getCaptchaKey())){
            throw new BusinessException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        if(!Objects.equals(captchaCode.toLowerCase(),stringRedisTemplate.opsForValue().get(loginVo.getCaptchaKey()))){
            throw new BusinessException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }

        String username = loginVo.getUsername();
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<SystemUser>();
        queryWrapper.eq(SystemUser::getUsername, username);
        SystemUser systemUser = systemUserMapper.selectOne(queryWrapper);
        if(Objects.isNull(systemUser)){
            throw new BusinessException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        if(Objects.equals(systemUser.getStatus(), BaseStatus.DISABLE)){
            throw new BusinessException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        if(!Objects.equals(systemUser.getPassword(), DigestUtils.md5Hex(loginVo.getPassword()))){
            throw new BusinessException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //JWT生成token
        return JwtUtil.createToken(systemUser.getId(), systemUser.getUsername());
    }


}
